package com.cedifie.lendapp.ui.loan.impl

import android.content.Context
import com.cedifie.lendapp.http.IHttpCallback
import com.cedifie.lendapp.mvp.presenter.MvpPresenter
import com.cedifie.lendapp.ui.loan.view.ICedLoanMainView
import com.cedifie.lendapp.utils.event.CedEventType
import com.cedifie.lendapp.utils.helper.SharedHelper
import com.cedifie.lendapp.utils.status.StatusRepository
import org.greenrobot.eventbus.EventBus
import org.json.JSONObject

/**
 * Cedifie
 * @Author wwdeng
 * @CreateTime: 2024/6/17 13:45
 * @Annotation：贷款主页面 Presenter 实现
 */
class CedLoanMainPresenterImpl(context: Context?) : MvpPresenter<ICedLoanMainView>(context) {

    private var loanModel: CedLoanMainModelImpl? = null
    init {
        loanModel = CedLoanMainModelImpl()
    }

    /**
     * 获取总流程
     */
    fun loadCedProcessAction(){
        loanModel?.loadCedProcessAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        //保存用户流程状态
                        if (obj != null) {
                            // TODO: 测试添加 Step 类型
//                            val allStepTitles = obj.optJSONArray("allStepTitles")
//                            allStepTitles.put("Ocr Info")
//                            allStepTitles.put("Face recognition")
//                            allStepTitles.put("Account Info")
//                            val allSteps = obj.optJSONArray("allSteps")
//                            allSteps.put("ocr_info")
//                            allSteps.put("sefie_info")
//                            allSteps.put("account_info")
//                            val steps = obj.optJSONArray("steps")
//                            steps.put("ocr_info")
//                            steps.put("sefie_info")
//                            steps.put("account_info")

                            SharedHelper.saveCedSPData(SharedHelper.CED_SPKEY.CEDIFIE_PROCESS,obj.toString())
                            //通知全局流程刷新完成
                            EventBus.getDefault().post(CedEventType.NOTICE.CED_PROCESS_REFRESHED)
                        }
                        //隐藏贷款所有页面
                        mvpView?.hideCedAllLoanPage()

                        if (obj != null){
                            // TODO: 2025-04-11 新增后置校验流程，判断是否需要校验手机号、
                            val smsFlag = obj.optString("smsFlag","")
                            val whatsappFlag = obj.optString("whatsappFlag","")
                            val telegramFlag = obj.optString("telegramFlag","")
                            val telegramBotFlag = obj.optString("telegramBotFlag","")
                            //需要校验手机号
                            if ("YES" == smsFlag || "YES" == whatsappFlag || "YES" == telegramFlag || "YES" == telegramBotFlag){
                                mvpView?.showCedCheckPhoneNumberPage()
                            }
                        }

                        //1、判断是否存在 订单状态
                        if (obj != null && obj.has("orderStatus")){
                            val orderStatus = obj.optString("orderStatus")
                            //待还款
                            if (StatusRepository(null,orderStatus).isRepayNow()){
                                mvpView?.showCedLoanRepayPage()
                                return
                            }
                            //放款中
                            if (StatusRepository(null,orderStatus).isDisbursing()){
                                obj.put("eventRemark","LoanAudit Page")
                                mvpView?.showCedLoadCommonPage(obj)
                                return
                            }
                            //申请贷款被拒绝
                            if (StatusRepository(null,orderStatus).isRejected()){
                                obj.put("eventRemark","LoanApplyFailed Page")
                                mvpView?.showCedLoadCommonPage(obj)
                                return
                            }
                        }

                        //2、判断是否存在 贷款申请状态
                        if (obj != null && obj.has("applyStatus")){
                            val applyStatus = obj.optString("applyStatus")
                            //审核中
                            if (StatusRepository(applyStatus).isReviewing()){
                                obj.put("eventRemark","LoanAudit Page")
                                mvpView?.showCedLoadCommonPage(obj)
                                return
                            }
                            //被拒绝
                            if (StatusRepository(applyStatus).isRejected()){
                                obj.put("eventRemark","LoanApplyFailed Page")
                                mvpView?.showCedLoadCommonPage(obj)
                                return
                            }
                        }

                        //3、直接显示贷款额度页面
                        if (obj != null){
                            val steps = obj.optJSONArray("steps")
                            steps?.let {
                                mvpView?.showCedLoanLimitPage(it.length() == 0)
                            }
                        }
                    }
                    else -> {
                        mvpView?.showCedToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView?.showCedToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView?.hideCedLoading()
            }
        })
    }
}